import type { Responses } from './types/assignment'
import { http } from '@/utils/request'

export const getAssignmentsByClassId = (classId: number): Promise<Responses['getAssignmentsByClassId']> => http.get(`/reading/classes/${classId}/assignments`)

export function createAssignment(data: {
  classId: number
  title: string
  assignmentDate: string
  requirementType: 'read' | 'audio'
  description: string
  bookIds: number[]
  excludedMemberIds?: number[]
  participantMemberIds?: number[]
  classIds?: number[]
}): Promise<Responses['createAssignment']> {
  return http.post(`/reading/assignments`, data)
}

export function createMultiClassAssignment(data: {
  classIds: number[]
  title: string
  assignmentDate: string
  requirementType: 'read' | 'audio'
  description: string
  bookIds: number[]
  participantMemberIds?: number[]
}): Promise<{ code: number; data: number[]; msg: string }> {
  return http.post(`/reading/assignments/multi-class`, data)
}

export const getAssignmentInfoByAssignmentId = (assignmentId: number): Promise<Responses['getAssignmentInfoByAssignmentId']> => http.get(`/reading/assignments/${assignmentId}`)

export const getCompletedInfosByAssignmentId = (assignmentId: number): Promise<Responses['getCompletedInfosByAssignmentId']> => http.get(`/reading/assignments/${assignmentId}/completions`)

export const getIncompletedInfosByAssignmentId = (assignmentId: number): Promise<Responses['getCompletedInfosByAssignmentId']> => http.get(`/reading/assignments/${assignmentId}/incomplete-students`)

export const deleteAssignmentByAssignmentId = (assignmentId: number): Promise<Responses['deleteAssignmentByAssignmentId']> => http.delete(`/reading/assignments/${assignmentId}`)

export function editAssignmentByAssignmentId(assignmentId: number, data: {
  title: string
  description: string
  requirementType: string
  assignmentDate?: string
  participantMemberIds?: number[]
}): Promise<Responses['editAssignmentByAssignmentId']> {
  return http.put(`/reading/assignments/${assignmentId}`, data)
}

export const getSelectableBooks = (language: 'chinese' | 'english'): Promise<Responses['getSelectableBooks']> => http.get(`/reading/assignments/books/selectable?language=${language === 'chinese' ? 'zh' : 'en'}`)

// 获取可选绘本集列表
export const getSelectableBookSets = (language?: 'chinese' | 'english'): Promise<Responses['getSelectableBookSets']> => {
  const params = language ? `?language=${language === 'chinese' ? 'zh' : 'en'}` : ''
  return http.get(`/reading/assignments/book-sets${params}`)
}

// 获取绘本集内的绘本列表
export const getBooksByBookSetId = (bookSetId: number): Promise<Responses['getBooksByBookSetId']> => http.get(`/reading/assignments/book-sets/${bookSetId}/books`)

// 提交作业完成记录
export function completeAssignment(assignmentId: number, data: {
  bookId: number
  completionType: string
  recordingUrl?: string
}) {
  return http.post(`/reading/assignments/${assignmentId}/complete`, data)
}

// 获取作业关联的班级列表（用于跨班级作业）
export const getAssignmentClasses = (assignmentId: number): Promise<{
  code: number
  data: Array<{
    classId: number
    className: string
  }>
  msg: string
}> => http.get(`/reading/assignments/${assignmentId}/classes`)
